mui.init({
	//下拉刷新
	pullRefresh: {
		container: "#refreshContainer", //待刷新区域标识，querySelector能定位的css选择器均可，比如：id、.class等
		up: {
			height: 50, // 可选.默认50.触发上拉加载拖动距离
			auto: true, // 可选,默认false.自动上拉加载一次
			contentrefresh: "正在加载...", // 可选，正在加载状态时，上拉加载控件上显示的标题内容
			contentnomore: '没有更多的数据了', // 可选，请求完毕若没有更多数据时显示的提醒内容；
			callback: pullupRefresh //加载数据方法 
		}
	}
});

//图片预览
mui.previewImage();

//搜索
$('#search_text').on('keyup', function(event) {
	if(event.keyCode == "13") {
		var search_text = $('#search_text').val();
		//pagenum = 0;
		mui.init();
		$('#landing_all_info_list').html('');
		$('#all_user_dynamic').html('');
		/*landing_page_user(search_text,pagenum);
		landing_page_dynamic(search_text,pagenum);*/
		document.activeElement.blur(); //关闭键盘
		$("#number").val(0);
		$("#search_centent").css("display", "block");
		landing_page_user(); //搜索用户
		pullupRefresh(); //搜索动态 
		user_info(); //用户信息
	}
});

/** 
 * 搜索-用户列表接口
 */
//请求方法
function landing_page_user() {
	var search_text = $('#search_text').val(); //搜索内容
	interfRequest.searchUser.data = {
		content: search_text,
		number: 0,
		pagesize: 4
	}
	socialRequest(interfRequest.searchUser)
}

//用户 成功函数回调
function searchUserHandle(data) { 
	var html = "";
	if(data.data.length == 0) {
		$(".landing_all_info").hide();
	} else {
		for(var i = 0; i < data.data.length; i++) {
			var head_img_url = data.data[i].head_img_url;
			html += '<div>';
			if(!head_img_url){
				html += '<img class="landing_all_info_img" src="../../images/log.png" />';
			}else{
				html += '<img class="landing_all_info_img" src="https://ypao.oss-cn-shenzhen.aliyuncs.com/' + data.data[i].head_img_url + '" />';
			} 
			html += '<p class="landing_all_info_title">' + data.data[i].nickname + '</p>';
			html += '</div>';
		}
		$('#landing_all_info_list').append(html);
	}

}

/**
 * 搜索-动态列表接口
 */
//请求方法
function landing_page_dynamic(search_text) {
	var number = $("#number").val(); 
	interfRequest.searchDynamic.data = {
		content: search_text,
		number: number,
		pagesize: 5
	}
	socialRequest(interfRequest.searchDynamic)
}

//动态成功函数回调
function searchDynamicHandle(data) { 
	var number = $("#number").val();
	if(number == 0) {
		if(data.data.length == 0) {
			$("#all_user_dynamic").html('<div class="landign_no_data2"><img style="width:30%" src="../../images/wdl.png"><p style="font-size:18px;">未查询到数据</p></div>');
		} else {
			dynamic(data);
		}
	} else {
		if(data.data.length == 0) {
			$("#isOver").val(true);
			mui('#refreshContainer').pullRefresh().endPullupToRefresh(true);
		} else {
			dynamic(data);
		}
	}
}

//动态数据处理
function dynamic(data) {

	var isOver = $("#isOver").val();
	if(isOver == "false") {
		var number = $("#number").val();
		var ye = Number(number) + Number(1);
		$("#number").val(ye++); 
		mui('#refreshContainer').pullRefresh().endPullupToRefresh(true); //停止正在加载
		mui('#refreshContainer').pullRefresh().enablePullupToRefresh(); //显示上拉加载文字
	} else {
		$("#isOver").val(true);
		mui('#refreshContainer').pullRefresh().endPullupToRefresh(true);
	}
	var html = "";
	for(var i = 0; i < data.data.length; i++) {
		var arr_img = data.data[i].dynamic_file; //图片数组
		var img_arr = arr_img.split(","); //图片 
		var is_valid = data.data[i].is_valid; //点赞
		var is_valid2 = data.data[i].is_valid2; //关注 
		var user_id = localStorage.getItem("user_id"); //user_id
		var head_img_url = data.data[i].head_img_url;
		html += '<li class="mui-table-view-cell all_user_back" id="' + data.data[i].dynamic_id + '">';
		html += '<div class="all_name_top">';
		html += '<div class="all_name_img" id="' + data.data[i].publisher + '">';
		if(!head_img_url){
			html += '<img src="../../images/log.png" />';
		}else{
			html += '<img src="https://ypao.oss-cn-shenzhen.aliyuncs.com/' + data.data[i].head_img_url + '" />';
		} 
		html += '</div>';
		html += '<span class="all_name_img_title">' + data.data[i].nickname + '</span>';
		html += '<p>' + data.data[i].release_time + '</p>';
		if(is_valid2 == 1) {
			html += '<span class="all_name_position follow">已关注</span>';
		}
		if(user_id == data.data[i].publisher) {

		} else {
			html += '<span class="all_name_position follow">+&nbsp;关注</span>';
		}
		html += '</div>';
		if(data.data[i].dynamic_type == 1) { //视频
			html += '<div class="video_center" style="position: relative;">';
			html += '<video name="media" width="100%" height="220px"   poster="https://ypao.oss-cn-shenzhen.aliyuncs.com/' + data.data[i].dynamic_file + '?spm=a2c4g.11186623.2.1.yjOb8V&x-oss-process=video/snapshot,t_1000,m_fast">';   //  视频封面				
			html += '<source src="https://ypao.oss-cn-shenzhen.aliyuncs.com/' + data.data[i].dynamic_file + '" type="video/mp4"></source>';
			html += '</video>';
			html += '<div class="img_video"><img src="../../images/ship.png" ></div>';
			html += '</div>';
			html += '<p class="all_vidio_text">' + data.data[i].content + '</p>';
		}
		if(data.data[i].dynamic_type == 2) { //图片
			html += '<p class="details_text">' + data.data[i].content + '</p>';
			html += '<div style="clear: both;"></div>'
			if(arr_img != "") {
				html += '<div class="mui-row landing_row_img">';
				for(var j = 0; j < img_arr.length; j++) {
					if(img_arr.length == 1) {
						html += '<div class="mui-col-xs-12 mui-col-sm-12">';
						html += '<img src="https://ypao.oss-cn-shenzhen.aliyuncs.com/' + img_arr[j] + '" " data-preview-src="" data-preview-group="' + data.data[i].dynamic_id + '1" />';
						html += '</div>';
					} else if(img_arr.length == 2 || img_arr.length == 4) {
						html += '<div class="mui-col-xs-6 mui-col-sm-6">';
						html += '<img src="https://ypao.oss-cn-shenzhen.aliyuncs.com/' + img_arr[j] + '" " data-preview-src="" data-preview-group="' + data.data[i].dynamic_id + '1" />';
						html += '</div>';
					} else if(img_arr.length == 3) {
						html += '<div class="mui-col-xs-4 mui-col-sm-4">';
						html += '<img src="https://ypao.oss-cn-shenzhen.aliyuncs.com/' + img_arr[j] + '" " data-preview-src="" data-preview-group="' + data.data[i].dynamic_id + '1" />';
						html += '</div>';
					} else if(img_arr.length > 4) {
						html += '<div class="mui-col-xs-4 mui-col-sm-4">';
						html += '<img src="https://ypao.oss-cn-shenzhen.aliyuncs.com/' + img_arr[j] + '" " data-preview-src="" data-preview-group="' + data.data[i].dynamic_id + '1" />';
						html += '</div>';
					}
				}
				html += '</div>';
			}
			html += '<div style="clear: both;"></div>';
		}
		html += '<div class="mui-row all_publish">';
		html += '<div class="mui-col-sm-4 mui-col-xs-4 publish_left">';
		if(is_valid == 1) {
			html += '<span class="iconfont icon-xihuan all_publish_icon all_icon_color"></span>';
		} else {
			html += '<span class="iconfont icon-xihuan all_publish_icon"></span>';
		}
		html += '<span class="all_publish_info">' + data.data[i].praise + '</span>';
		html += '</div>';
		html += '<div class="mui-col-sm-4 mui-col-xs-4 comment">';
		html += '<span class="iconfont icon-comment all_publish_icon1"></span>';
		html += '<span class="all_publish_info">' + data.data[i].comment_number + '</span>';
		html += '</div>';
		html += '<div class="mui-col-sm-4 mui-col-xs-4 publish_right">';
		html += '	<a href="#sheet" ><span class="iconfont icon-gengduo all_publish_icon1"></span></a>';
		html += '</div>';
		html += '</div>';
		html += '</li>';
	}
	$('#all_user_dynamic').append(html);
	//图片大小处理
	var width4 = $(".landing_row_img .mui-col-xs-4").width();
	$(".landing_row_img .mui-col-xs-4").height(width4);
	var width6 = $(".landing_row_img .mui-col-xs-6").width();
	$(".landing_row_img .mui-col-xs-6").height(width6);
}

//下拉加载
function pullupRefresh() {
	setTimeout(function() {
		mui('#refreshContainer').pullRefresh().disablePullupToRefresh();
		setTimeout(function() {
			var isOver = $("#isOver").val(); //加载判断
			var search_text = $('#search_text').val(); //搜索内容
			mui('#refreshContainer').pullRefresh().endPullupToRefresh((isOver)); //参数为true代表没有更多数据了。  
			landing_page_dynamic(search_text); //搜索动态
		}, 500);
	}, 500);
}

//切换跳转
/*mui('body').on('tap', '.mui-control-item', function() {
	window.top.location.href = this.href;
});*/

//查看搜索用户 
mui('body').on('tap', '.user_btn,#landing_more', function() {
	var search_text = $('#search_text').val(); 
	mui.openWindow({
		url: 'landing_page2.html',
		id: 'landing_page2',
		extras: {
			search: search_text,
		},
		createNew: true
	});
	//window.top.location.href = 'landing_page2.html';

});

//查看搜索动态
mui('body').on('tap', '.dynamic_btn', function() {
	var search_text = $('#search_text').val(); 
	mui.openWindow({
		url: 'landing_page3.html',
		id: 'landing_page3',
		extras: {
			search: search_text,
		},
		createNew: true
	});
	//window.top.location.href = 'landing_page2.html';

});

//取消
mui('body').on('tap', '.search_right', function() {
	window.top.location.href = '../../Recommend.html';
});

//查看用户
mui('body').on('tap', '.all_name_img', function() {
	var mag = $("#mag").val(); //提示  
	var id = $(this).attr("id");
	//var mag = $("#mag").val();//提示  
	if(mag == "请先登录") { //判断是否登录 
		var btnArray = ['否', '是'];
		mui.confirm(mag, '提示', btnArray, function(e) {
			if(e.index == 1) {
				/*mui.openWindow({
					url: '../user/login.html',
					id: 'login',
				});*/
				window.top.location.href = '../user/login.html';
			} else {}
		})
	} else {
		mui.openWindow({
			url: '../video/fan_pages.html',
			extras: {
				publisher: id,
			},
			createNew: true
		});
	}
})

//点赞动态
mui('body').on('tap', '.publish_left', function() {
	var mag = $("#mag").val(); //提示   
	if(mag == "请先登录") { //判断是否登录 
		var btnArray = ['否', '是'];
		mui.confirm(mag, '提示', btnArray, function(e) {
			if(e.index == 1) {
				/*mui.openWindow({
					url: '../user/login.html',
					id: 'login',
				});*/
				window.top.location.href = '../user/login.html';
			} else {}
		})
	} else {
		$(this).addClass("zan").parents().siblings().children(".all_publish").find(".publish_left").removeClass("zan");
		var dynamic_id = $(this).parents(".all_user_back").attr("id"); //动态id 
		var user_id = localStorage.getItem("user_id"); //user_id 
		interfRequest.dynamic_label.data = {
			user_id: user_id,
			dynamic_id: dynamic_id, //true	普通参数	int		动态id
		}
		socialRequest(interfRequest.dynamic_label); //异常处理 

	}

});

/*点赞*/
function dynamic_labelHandle(data) { 
	if(data.error_code == 10000) {
		$(".zan").find(".iconfont").css("color", "#e51739");
		var dz_num = $(".zan").find(".all_publish_info").text(); //赞数 
		$(".zan").find(".all_publish_info").text(parseInt(dz_num) + 1);
	} else {
		mui.toast("该条动态已赞");
	}
}

//查看详情     评论 
mui('body').on('tap', '.all_vidio_text,.details_text,.comment', function() {
	var mag = $("#mag").val(); //提示   
	if(mag == "请先登录") { //判断是否登录 
		var btnArray = ['否', '是'];
		mui.confirm(mag, '提示', btnArray, function(e) {
			if(e.index == 1) {
				/*mui.openWindow({
					url: '../user/login.html',
					id: 'login',
				});*/
				window.top.location.href = '../user/login.html';
			} else {}
		})
	} else {
		var id = $(this).parents(".all_user_back").attr("id"); //动态id 
		mui.openWindow({
			url: 'graphic_details_page.html',
			id: 'graphic_details_page',
			extras: {
				dynamic_id: id,
			},
			createNew: true
		});
	}
});

//关注用户
mui('body').on('tap', '.all_name_position', function() {
	var mag = $("#mag").val(); //提示  
	if(mag == "请先登录") { //判断是否登录  
		var btnArray = ['否', '是'];
		mui.confirm(mag, '提示', btnArray, function(e) {
			if(e.index == 1) {
				/*mui.openWindow({
					url: '../user/login.html',
					id: 'login',
				});*/
				window.top.location.href = '../user/login.html';
			} else {}
		})
	} else {
		var publisher = $(this).siblings(".all_name_img").attr("id"); //用户id  
		var user_id = localStorage.getItem("user_id"); //user_id
		$(this).addClass("follow").parents().siblings().children(".all_name_top").find(".all_name_position").removeClass("follow");
		interfRequest.concern.data = {
			user_id: user_id,
			passive_user_id: publisher //true	普通参数	int		我关注的人id
		}
		socialRequest(interfRequest.concern); //异常处理   
	}
});

//更多
mui('body').on('tap', '.publish_right', function() {
	$(this).addClass("more").parents().siblings().children(".all_publish").find(".publish_right").removeClass("more");
});

//举报
mui('body').on('tap', '#report', function() {
	var mag = $("#mag").val(); //提示  
	var dynamicid = $(".more").parents(".all_user_back").attr("id"); //动态id 
	var passiveid = $(".more").parent().siblings(".all_name_top").find(".all_name_img").attr("id"); //动态用户id
	var username = $(".more").parent().siblings(".all_name_top").find(".all_name_img_title").text(); //动态用户昵称  
	if(mag == "请先登录") { //判断是否登录  
		var btnArray = ['否', '是'];
		mui.confirm(mag, '提示', btnArray, function(e) {
			if(e.index == 1) {
				/*mui.openWindow({
					url: '../user/login.html',
					id: 'login',
				});*/
				window.top.location.href = '../user/login.html';
			} else {}
		})
	} else {
		mui.openWindow({ //举报页面
			url: 'Report.html',
			id: 'Report',
			extras: {
				dynamic_id: dynamicid,
				passive_id: passiveid,
				passive_username: username
			},
			createNew: true
		});
	}
});

//返回到首页
mui.back = function() {
	window.top.location.href = '../../Recommend.html';
}

//用户信息
function user_info() {
	interfRequest.user_info.data = {
		refresh: 1 //true	普通参数	string		刷新状态1代表刷新
	}
	socialRequest(interfRequest.user_info); //异常处理 
}

mui.plusReady(function() {
	setTimeout(function() {
		var self = plus.webview.currentWebview();
		$('#search_text').val(self.search);
		$("#number").val(0);
		$("#search_centent").css("display", "block");
		landing_page_user(); //搜索用户
		pullupRefresh(); //搜索动态 
		user_info(); //用户信息
	}, 50)
});

//视频播放
mui('body').on('tap', '.img_video', function() {
	var videoMedia = $(this).siblings().children("source").attr("src");
	var bu = document.getElementById("videoMedia"); 
	bu.src = videoMedia;
	bu.play();  
	$(".video_ji").show();   
}); 

//关闭视频
mui('body').on('tap', '.back', function() {
	$(".video_ji").hide();
	var bu = document.getElementById("videoMedia");
	bu.innerHTML = '';
	bu.pause();
});
